<?php

class CodeController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    public function indexAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $model = new CodeModel();
        $this->view->phpcode = $model->getContent($sid)->code;
    }

    public function modifyAction() {
        $model = new CodeModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $content = $model->getContent($sid);
        $this->view->code = $content->code;
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            $content = array('code' => $formData['code']);
            $model->updateContent($content, $sid);
            $this->_redirect('/pages/modify/id/' . $sid);
        } else {

        }
    }

    public function addAction() {
        $model = new CodeModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $model->addContent(array('sid' => $sid, 'code' => ''));
        $this->_redirect('/pages/section/id/' . $pid);
    }

}
